<!--# layout("/templates/layout/layout_main.html",{"title":"权限设置","nameSpace":"permission_admin","breadcrumbs":[{"name":"权限管理","url":"permission/list"},{"name":"权限设置"}]}){ #-->
	<div class="row" style="margin-left:5px;width:99%">
		<div class="col-md-5 no-padding">
			<div class="tabbable">
				<ul class="nav nav-tabs padding-12 tab-color-blue background-blue" id="myTab4">
					<li class="active">
						<a data-toggle="tab" href="#rolePermission" aria-expanded="true">角色权限设置</a>
					</li>
	
					<li class="">
						<a data-toggle="tab" href="#userPermission" aria-expanded="false">用户权限设置</a>
					</li>
				</ul>
	
				<div class="tab-content no-padding ">
					<div id="rolePermission" class="tab-pane active ">
						<table id="roleTable" class="table  table-hover general-table table-striped table-bordered no-margin-bottom">
							<thead>
								<thead>
								<tr>
									<th colspan="4">
										<div class="row">
											<div class="col-md-9">
												<div class="input-group">
													<input type="text" class="form-control" placeholder="请输入关键词进行检索..." style="font-weight: 500" name="key"
														value="" data-default="Search..." >
													<span class="input-group-addon search-btn" style="cursor: pointer;"><i class="fa fa-search"></i> 查询</span>
												</div>
											</div>
										</div>
									</th>
								</tr>
								<tr>
									<th class="center">序号</th>
									<th class="center">角色KEY</th>
									<th class="center">角色名称</th>
									<th class="center">操作</th>
								</tr>
							</thead>
							<tbody>
							</tbody>
						</table>
					</div>
	
					<div id="userPermission" class="tab-pane">
						<table id="userTable" class="table  table-hover general-table table-striped table-bordered no-margin-bottom">
							<thead>
								<tr>
									<th colspan="5">
										<div class="row">
											<div class="col-md-9">
												<div class="input-group">
													<input type="text" class="form-control" placeholder="请输入关键词进行检索..." style="font-weight: 500" name="key"
														value="" >
													<span class="input-group-addon search-btn" style="cursor: pointer;"><i class="fa fa-search"></i> 查询</span>
												</div>
											</div>
										</div>
									</th>
								</tr>
								<tr>
									<th class="center">序号</th>
									<th class="center">登录名</th>
									<th class="center">真实姓名</th>
									<th class="center">操作</th>
								</tr>
							</thead>
							<tbody>
								<!-- <tr class="main_info">
									<td colspan="5" class="center">没有相关数据</td>
								</tr> -->
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<div class="col-md-7" style="padding-right:0px;">
			<div class="tabbable">
				<ul class="nav nav-tabs padding-12 tab-color-blue background-blue" id="menuTab">
					<li class="active">
						<a data-toggle="tab" href="#menu_permission" aria-expanded="true">所有权限[角色/用户名][名称]</a>
					</li>
				</ul>
	
				<div class="tab-content">
					<div id="menu_permission" class="tab-pane active" style="padding-left:100px;">
						<ul id="menuTree" class="ztree"></ul>
					</div>
				</div>
			</div>
		</div>
	</div>
		
	<div class="" id="dialogs">
		<div id="editDialog" class="hidden">
		</div>
	</div>
	<script src="${base}/assets/biz-logic/js/ztree/jquery.ztree.all.min.js"></script>
	<script type="text/javascript">
	$(function($) {
		var menuTreeSetting = {
				async: {
					enable: true,
					url:getRootPath() + "/permission/getTree",
					autoParam:["id", "name"],
					type:'post'
				},
				check: {
					enable: true,
					autoCheckTrigger: false,
					chkStyle: "checkbox",
					chkboxType: { "Y": "", "N": "" },
					nocheckInherit: true,
					chkDisabledInherit: true
				},
				callback:{
					onNodeCreated:function(e, treeid, treeNode){
					},
					onAsyncSuccess:function(event, treeId, treeNode, msg){
						var treeObj = $.fn.zTree.getZTreeObj("menuTree");
						treeObj.expandAll(true);
					},
					beforeCheck: function(treeId, treeNode) {
						var dataObj = $('#menuTab').data();
						console.info(dataObj);
						if(!dataObj || dataObj == null || !dataObj.id ){
							layer.msg("请先选择设置对象！");
							return false;
						}
						return true;
					},
					onCheck: function(event, treeId, treeNode){
						var dataObj = $('#menuTab').data();
						$.ajax({
							type:'post',
							url:getRootPath() + '/userRole/setPermission.do',
							data:{userId:dataObj.id, roleId:dataObj.id,permissionId:treeNode.id,status:treeNode.checked ? "Y" : "N" , op: dataObj.op},
							dataType:'json',
							success:function(result) {
								if (result.operationState == 'SUCCESS') {
									
								}else {
									layer.msg(result.data.reason);
								}
							}
						});
					}
				}
		};
		var ztree = $.fn.zTree.init($("#menuTree"), menuTreeSetting);
		loadRoles('');
		loadUsers('');
		
		$('.search-btn').on("click", function(){
			var key = $("input[name=key]:visible").val();
			if($(".tab-pane.active").attr("id") == 'rolePermission'){
				loadRoles(key);
			}
			if($(".tab-pane.active").attr("id") == 'userPermission'){
				loadUsers(key);
			}
		});
		
		$('.nav-tabs li,.search-btn').on("click", function(){
			$('#menuTab li.active a').html("所有权限[角色/用户名][名称]");
			//$.fn.zTree.init($("#menuTree"), menuTreeSetting);
			var treeObj = $.fn.zTree.getZTreeObj("menuTree");
			treeObj.checkAllNodes(false);
			$('#menuTab').removeData();
		});
	});
	function loadRoles(key){
		$.ajax({
			type:'post',
			url:getRootPath() + '/role/all',
			data:{key:key},
			dataType:'json',
			success:function(result) {
			if (result.operationState == 'SUCCESS') {
				$('#roleTable tbody').html('');
				if(result.data.roles.length > 0){
					$.each(result.data.roles, function(i,role){
						var rhtml = '<tr data-id="'+ role.id +'" data-name="'+ role.description +'" >';
						rhtml += '<td class="center">'+ (i + 1) +'</td>';
						rhtml += '<td>'+ role.name +'</td>';
						rhtml += '<td>'+ role.description +'</td>';
						rhtml += '<td class="center"><a class="btn btn-xs btn-warning" href="javascript:toSetting('+ role.id +')"><i class="ace-icon fa fa-cogs bigger-120" title="权限设置"></i></a></td>';
						rhtml += '</tr>';
						$('#roleTable tbody').append(rhtml);
					});
				}else{
					$('#roleTable tbody').append('<tr class="main_info"><td colspan="4" class="center">没有相关数据</td></tr>');
				}
			} else {
				layer.msg(result.data.reason);
			}
		}});
	}
	
	//加载用户数据
	function loadUsers(key){
		var currPager = $('#userTable').data();
		$.ajax({
			type:'post',
			url:getRootPath() + '/user/getList',
			data:{pagerNumber:currPager.pn || 0 , pageSize:currPager.ps || 0,key:key},
			dataType:'json',
			success:function(result) {
			if (result.operationState == 'SUCCESS') {
				$('#userTable tbody').html('');
				var pager = result.data.pager ;
				var users = pager.pageRows ;
				if(users.length > 0){
					$.each(users, function(i,user){
						var rhtml = '<tr data-id="'+ user.id +'" data-name="'+ user.name +'" >';
						rhtml += '<td class="center">'+ ((pager.pageNumber - 1)*pager.pageSize  + (i + 1)) +'</td>';
						rhtml += '<td>'+ user.name +'</td>';
						rhtml += '<td>'+ user.realName +'</td>';
						rhtml += '<td class="center"><a class="btn btn-xs btn-warning" href="javascript:toSetting('+ user.id +')"><i class="ace-icon fa fa-cogs bigger-120" title="权限设置"></i></a></td>';
						rhtml += '</tr>';
						$('#userTable tbody').append(rhtml);
					});
					$('#userTable tbody').append('<tr><td colspan="4"><ul class="pagination"></ul></td></tr>');
					$('.pagination')._pagination({pn:pager.pageNumber,ps:pager.pageSize,total:pager.recordCount});
					$('#userTable').data($(".pagination").data());
				}else{
					$('#userTable tbody').append('<tr class="main_info"><td colspan="4" class="center">没有相关数据</td></tr>');
				}
			} else {
				layer.msg(result.data.reason);
			}
		}});
	}
	
	function toSetting(id){
		var data = $('.tab-pane.active table tr[data-id='+ id +']').data();
		if($(".tab-pane.active").attr("id") == 'rolePermission'){
			$('#menuTab li.active a').html("所有权限[角色]["+ data.name +"]")
			data.op = 'rp';
			$('#menuTab').data(data);
			$.ajax({
				type:'post',
				url:getRootPath() + '/userRole/getPermission',
				data:{roleId:data.id,op:data.op},
				dataType:'json',
				success:function(result) {
					if (result.operationState == 'SUCCESS') {
						var treeObj = $.fn.zTree.getZTreeObj("menuTree");
						treeObj.checkAllNodes(false);
						$.each(result.data.list, function(i, perm){
							var node = treeObj.getNodeByParam("id", perm.id, null);
							treeObj.checkNode(node, true,false);
						});
					}else {
						layer.msg(result.data.reason);
					}
				}
			});	
		}
		if($(".tab-pane.active").attr("id") == 'userPermission'){
			$('#menuTab li.active a').html("所有权限[用户名]["+ data.name +"]")
			data.op = 'up';
			$('#menuTab').data(data);
			$.ajax({
				type:'post',
				url:getRootPath() + '/userRole/getPermission',
				data:{userId:data.id,op:data.op},
				dataType:'json',
				success:function(result) {
					if (result.operationState == 'SUCCESS') {
						var treeObj = $.fn.zTree.getZTreeObj("menuTree");
						treeObj.checkAllNodes(false);
						$.each(result.data.list, function(i, perm){
							var node = treeObj.getNodeByParam("id", perm.id, null);
							treeObj.checkNode(node, true,false);
						});
					} else {
						layer.msg(result.data.reason);
					}
				}
			});	
		}
	}
</script>
<!--# } #-->

 